package com.example.leave.common;

import com.example.leave.dao.UserRepository;
import com.example.leave.entity.User;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@DependsOn("springContextUtil")
public class JwtUtil {
    public static User getUserFromToken() {
        UserRepository userRepository = (UserRepository) SpringContextUtil.getBean("userRepository");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        return userRepository.findById(request.getHeader("token")).orElse(new User());

    }
}
